package com.stiove.whwh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.stiove.whwh.dao.mapper.*;
import com.stiove.whwh.dao.pojo.*;
import com.stiove.whwh.service.OrderService;
import com.stiove.whwh.service.ThreadService;
import com.stiove.whwh.service.TicketService;
import com.stiove.whwh.utils.OssUtils;
import com.stiove.whwh.utils.UserThreadLocal;
import com.stiove.whwh.vo.*;
import com.stiove.whwh.vo.params.OrderParams;
import com.stiove.whwh.vo.params.TicketParams;
import lombok.Data;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Data
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private TicketMapper ticketMapper;
    @Autowired
    private UserOrderMapper userOrderMapper;
    @Autowired
    private UserMapper userMapper;


    @Autowired
    private Env env;
    @Autowired
    private ThreadService threadService;

    @Autowired
    private TicketServiceImpl ticketServiceImpl;


    @Override
    public Result creatOrder(OrderParams orderParams) {
        Long user_id = UserThreadLocal.get().getId();
        Ticket ticketInfo = ticketMapper.selectTicketById(orderParams.getTicket_id());

        UserOrder userOrder = new UserOrder();
        userOrder.setState(1);
        userOrder.setTicketId(orderParams.getTicket_id());
        userOrder.setNum(orderParams.getNum());
        userOrder.setUserId(user_id);
        userOrder.setUpdateTime(System.currentTimeMillis());
        userOrder.setPrice(ticketInfo.getPrice() * orderParams.getNum());

        userOrderMapper.insert(userOrder);
        return   new Result(Result.SUCCESS,"cg");
        }

    @Override
    public Result getAdminOrder(int page) {
        LambdaQueryWrapper<UserOrder> wrapper = Wrappers.lambdaQuery();
        wrapper.isNull(UserOrder::getDeleteTime);

        if (page == 0) page = 1;
        Page<UserOrder> pageNum = new Page<>(page, 20);
        Page<UserOrder> ticketPage = userOrderMapper.selectPage(pageNum, wrapper);
        if (ticketPage.getRecords() == null){
            return new Result(Result.SUCCESS,"cg");
        }

        ArrayList<OrderVo> orderVos = new ArrayList<>();

        for (UserOrder order : ticketPage.getRecords()) {
            TicketImgVo ticketInfo = ticketMapper.selectTicketInfoById(order.getTicketId());
            OrderVo orderVo = new OrderVo();

            orderVo.setUsername(userMapper.getUserInfo(order.getUserId()).getUsername());
            orderVo.setTicket_id(ticketInfo.getId());
            orderVo.setCity(ticketInfo.getCity());
            orderVo.setTitle(ticketInfo.getTitle());
            orderVo.setImg_url(ticketInfo.getImgUrl());
            orderVo.setId(order.getId());
            orderVo.setPrice(order.getPrice());
            orderVo.setNum(order.getNum());
            orderVos.add(orderVo);
        }

        return new Result(Result.SUCCESS,"成功",orderVos);

    }
}
